#include "InSdkIniDev.h"
#include "InSdkIniConfig.h"

#define DEF_TAG "IniDev"

#define DEF_INSDK_INI_DEV_TYPE_INT      "dev_int"
#define DEF_INSDK_INI_DEV_TYPE_STR      "dev_str"

pthread_mutex_t g_InSdkIniDev_Mutex;

StInSdkIniCfgKeyInfo g_InSdkIniDev_info[INI_IOT_MAX] = {
    {INI_DEV_INT_SPK_VOLUME, "dev_int:spk_volume"},
    {INI_DEV_INT_VIDEO_OSD, "dev_int:video_osd"},
    {INI_DEV_INT_VIDEO_FLIP, "dev_int:video_flip"},
    {INI_DEV_INT_NIGHT_MODE, "dev_int:night_mode"},
    {INI_DEV_INT_MD_STATUS, "dev_int:md_status"},
    {INI_DEV_INT_MD_SENSITIVITY, "dev_int:md_sensitivity"},
    {INI_DEV_INT_MD_TIME_SWITCH, "dev_int:md_time_switch"},
    {INI_DEV_INT_ALARM_ZONE, "dev_int:alarm_zone"},
    {INI_DEV_INT_IPC_CONTRAST, "dev_int:ipc_contrast"},
    {INI_DEV_INT_IPC_BRIGHT, "dev_int:ipc_bright"},
    {INI_DEV_INT_SLEEP_MODE, "dev_int:sleep_mode"},
    {INI_DEV_INT_ANTI_FLICKER, "dev_int:anti_flicker"},
    {INI_DEV_STR_MOTION_TIME, "dev_str:motion_time"},
    {INI_DEV_STR_ALARM_ZONE_DRAW, "dev_str:alarm_zone_draw"},
};

Com_void InSdkIniIot_SetDefault(Com_void)
{
    struct config *conf = conf_load(DEF_INI_DEV_CONFIG);
    pStInSdkIniCfgKeyInfo dev_info = g_InSdkIniDev_info;

    if ((conf_get_string(conf, dev_info[INI_DEV_INT_SPK_VOLUME].string) == NULL) ||
        (conf_get_string(conf, dev_info[INI_DEV_STR_MOTION_TIME].string) == NULL))
    {
        Ext_InSdkIniCfg_Recover(conf, DEF_INSDK_INI_DEV_TYPE_INT);
        Ext_InSdkIniCfg_Recover(conf, DEF_INSDK_INI_DEV_TYPE_STR);

        conf_set_string(conf, dev_info[INI_DEV_INT_SPK_VOLUME].string, "6");
        conf_set_string(conf, dev_info[INI_DEV_INT_VIDEO_OSD].string, "1");
        conf_set_string(conf, dev_info[INI_DEV_INT_VIDEO_FLIP].string, "0");
        conf_set_string(conf, dev_info[INI_DEV_INT_NIGHT_MODE].string, "0");
        conf_set_string(conf, dev_info[INI_DEV_INT_MD_STATUS].string, "0");
        conf_set_string(conf, dev_info[INI_DEV_INT_MD_SENSITIVITY].string, "0");
        conf_set_string(conf, dev_info[INI_DEV_INT_MD_TIME_SWITCH].string, "0");
        conf_set_string(conf, dev_info[INI_DEV_INT_ALARM_ZONE].string, "0");
        conf_set_string(conf, dev_info[INI_DEV_INT_IPC_CONTRAST].string, "50");
        conf_set_string(conf, dev_info[INI_DEV_INT_IPC_BRIGHT].string, "50");
        conf_set_string(conf, dev_info[INI_DEV_INT_SLEEP_MODE].string, "0");
        conf_set_string(conf, dev_info[INI_DEV_INT_ANTI_FLICKER].string, "0");
        conf_set_string(conf, dev_info[INI_DEV_STR_MOTION_TIME].string, "00002359");
        conf_set_string(conf, dev_info[INI_DEV_STR_ALARM_ZONE_DRAW].string, "off");

        conf_save(conf);
    }

    conf_unload(conf);
}

Com_void Ext_InSdkIniIot_SetData(EnInSdkIniDevIndex dp, pUnInSdkIniCfgKeyData udata)
{
    pthread_mutex_lock(&g_InSdkIniDev_Mutex);

    char buff[280];
    memset(buff, 0, sizeof(buff));

    struct config *conf = conf_load(DEF_INI_DEV_CONFIG);
    pStInSdkIniCfgKeyInfo dev_info = g_InSdkIniDev_info;

    if(dp < INI_DEV_STR_MOTION_TIME)
    {
        snprintf(buff, sizeof(buff), "%d", udata->val);
        conf_set_string(conf, dev_info[dp].string, buff);
    }
    else if (dp < INI_IOT_MAX)
    {
        conf_set_string(conf, dev_info[dp].string, udata->str);
    }

    conf_save(conf);
    conf_unload(conf);
    pthread_mutex_unlock(&g_InSdkIniDev_Mutex);
}

Com_void Ext_InSdkIniIot_GetData(EnInSdkIniDevIndex dp, pUnInSdkIniCfgKeyData udata)
{
    pthread_mutex_lock(&g_InSdkIniDev_Mutex);

    struct config *conf = conf_load(DEF_INI_DEV_CONFIG);
    pStInSdkIniCfgKeyInfo dev_info = g_InSdkIniDev_info;

    if(dp < INI_DEV_STR_MOTION_TIME)
    {
        udata->val = conf_get_int(conf, dev_info[dp].string);
    }
    else if (dp < INI_IOT_MAX)
    {
        strcpy(udata->str, "1");
        if (conf_get_string(conf, dev_info[dp].string) != NULL)
        {
            strcpy(udata->str, conf_get_string(conf, dev_info[dp].string));
        }
    }

    conf_unload(conf);
    pthread_mutex_unlock(&g_InSdkIniDev_Mutex);
}

Com_void Ext_InSdkIniIot_Init(Com_void)
{
    pthread_mutex_init(&g_InSdkIniDev_Mutex, NULL);
    Ext_InSdkIniCfg_Check(DEF_INI_DEV_CONFIG);
    InSdkIniIot_SetDefault();
}

Com_void Ext_InSdkIniIot_Exit(Com_void)
{
    pthread_mutex_destroy(&g_InSdkIniDev_Mutex);
}